<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API 测试页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .test-section {
            margin: 20px 0;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .test-button {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 3px;
            cursor: pointer;
            margin: 5px;
        }
        .test-button:hover {
            background: #0056b3;
        }
        .result {
            margin-top: 10px;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 3px;
            white-space: pre-wrap;
        }
        .error {
            background: #f8d7da;
            color: #721c24;
        }
        .success {
            background: #d4edda;
            color: #155724;
        }
    </style>
</head>
<body>
    <h1>API 接口测试页面</h1>
    
    <div class="test-section">
        <h3>1. 用户登录测试</h3>
        <button class="test-button" onclick="testLogin()">测试登录</button>
        <div id="login-result" class="result"></div>
    </div>
    
    <div class="test-section">
        <h3>2. 图片上传测试</h3>
        <input type="file" id="portrait-file" accept="image/*" placeholder="选择人物照">
        <input type="file" id="exhibition-files" accept="image/*" multiple placeholder="选择看展照">
        <button class="test-button" onclick="testUpload()">测试上传</button>
        <div id="upload-result" class="result"></div>
    </div>
    
    <div class="test-section">
        <h3>3. 图片分析测试</h3>
        <input type="number" id="portrait-id" placeholder="人物照ID">
        <input type="text" id="exhibition-ids" placeholder="看展照ID列表（逗号分隔）">
        <button class="test-button" onclick="testAnalysis()">测试分析</button>
        <div id="analysis-result" class="result"></div>
    </div>
    
    <div class="test-section">
        <h3>4. 生成图片测试</h3>
        <input type="number" id="analysis-id" placeholder="分析ID">
        <button class="test-button" onclick="testGenerate()">测试生成</button>
        <div id="generate-result" class="result"></div>
    </div>
    
    <div class="test-section">
        <h3>5. 选择图片测试</h3>
        <input type="number" id="gen-image-id" placeholder="生成的图片ID">
        <button class="test-button" onclick="testSelect()">测试选择</button>
        <div id="select-result" class="result"></div>
    </div>
    
    <div class="test-section">
        <h3>6. 生成文案测试</h3>
        <input type="number" id="selection-id" placeholder="选择ID">
        <button class="test-button" onclick="testText()">测试文案生成</button>
        <div id="text-result" class="result"></div>
    </div>
    
    <div class="test-section">
        <h3>7. 创建视频任务测试</h3>
        <input type="number" id="video-selection-id" placeholder="选择ID">
        <input type="number" id="ref-image-id" placeholder="参考图片ID">
        <button class="test-button" onclick="testVideoTask()">测试创建任务</button>
        <div id="video-task-result" class="result"></div>
    </div>
    
    <div class="test-section">
        <h3>8. 查询任务状态测试</h3>
        <input type="number" id="query-user-id" placeholder="用户ID">
        <input type="number" id="query-task-id" placeholder="任务ID">
        <button class="test-button" onclick="testQueryStatus()">测试查询状态</button>
        <div id="query-result" class="result"></div>
    </div>

    <script>
        // 模拟uni-app环境
        const mockUni = {
            request: async (options) => {
                console.log('Mock uni.request:', options);
                // 这里可以返回模拟数据
                return {
                    statusCode: 200,
                    data: {
                        code: 1,
                        msg: 'success',
                        data: { message: '模拟成功响应' }
                    }
                };
            },
            uploadFile: async (options) => {
                console.log('Mock uni.uploadFile:', options);
                return {
                    data: JSON.stringify({
                        code: 1,
                        msg: 'success',
                        data: { message: '模拟上传成功' }
                    })
                };
            },
            getStorageSync: (key) => {
                return localStorage.getItem(key);
            },
            setStorageSync: (key, value) => {
                localStorage.setItem(key, value);
            }
        };

        // 全局替换uni对象
        if (typeof uni === 'undefined') {
            window.uni = mockUni;
        }

        // 测试函数
        function showResult(elementId, data, isError = false) {
            const element = document.getElementById(elementId);
            element.textContent = JSON.stringify(data, null, 2);
            element.className = `result ${isError ? 'error' : 'success'}`;
        }

        async function testLogin() {
            try {
                const response = await fetch('http://14.103.244.167:8080/user/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        code: 'test_code_123'
                    })
                });
                const result = await response.json();
                showResult('login-result', result);
            } catch (error) {
                showResult('login-result', { error: error.message }, true);
            }
        }

        async function testUpload() {
            try {
                const formData = new FormData();
                const portraitFile = document.getElementById('portrait-file').files[0];
                const exhibitionFiles = document.getElementById('exhibition-files').files;
                
                if (portraitFile) {
                    formData.append('portraitImage', portraitFile);
                }
                
                for (let file of exhibitionFiles) {
                    formData.append('exhibitionImages', file);
                }
                
                const response = await fetch('http://14.103.244.167:8080/user/image/upload', {
                    method: 'POST',
                    headers: {
                        'token': 'eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3NTU1MzA2MDcsInVzZXJJZCI6MX0.S4X0YotcNiRqWiWl1DjCWpbflUHdBr1zGwg1uAtyxRA'
                    },
                    body: formData
                });
                const result = await response.json();
                showResult('upload-result', result);
            } catch (error) {
                showResult('upload-result', { error: error.message }, true);
            }
        }

        async function testAnalysis() {
            try {
                const portraitId = document.getElementById('portrait-id').value;
                const exhibitionIds = document.getElementById('exhibition-ids').value.split(',').map(id => parseInt(id.trim()));
                
                const response = await fetch('http://14.103.244.167:8080/user/image/analysis', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'token': 'eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3NTU1MzA2MDcsInVzZXJJZCI6MX0.S4X0YotcNiRqWiWl1DjCWpbflUHdBr1zGwg1uAtyxRA'
                    },
                    body: JSON.stringify({
                        portraitImageId: parseInt(portraitId),
                        exhibitionImageIds: exhibitionIds
                    })
                });
                const result = await response.json();
                showResult('analysis-result', result);
            } catch (error) {
                showResult('analysis-result', { error: error.message }, true);
            }
        }

        async function testGenerate() {
            try {
                const analysisId = document.getElementById('analysis-id').value;
                
                const response = await fetch('http://14.103.244.167:8080/user/image/generate', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'token': 'eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3NTU1MzA2MDcsInVzZXJJZCI6MX0.S4X0YotcNiRqWiWl1DjCWpbflUHdBr1zGwg1uAtyxRA'
                    },
                    body: JSON.stringify({
                        analysisId: parseInt(analysisId)
                    })
                });
                const result = await response.json();
                showResult('generate-result', result);
            } catch (error) {
                showResult('generate-result', { error: error.message }, true);
            }
        }

        async function testSelect() {
            try {
                const genImageId = document.getElementById('gen-image-id').value;
                
                const response = await fetch('http://14.103.244.167:8080/user/image/select', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'token': 'eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3NTU1MzA2MDcsInVzZXJJZCI6MX0.S4X0YotcNiRqWiWl1DjCWpbflUHdBr1zGwg1uAtyxRA'
                    },
                    body: JSON.stringify({
                        genImageId: parseInt(genImageId)
                    })
                });
                const result = await response.json();
                showResult('select-result', result);
            } catch (error) {
                showResult('select-result', { error: error.message }, true);
            }
        }

        async function testText() {
            try {
                const selectionId = document.getElementById('selection-id').value;
                
                const response = await fetch('http://14.103.244.167:8080/user/text/generate', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'token': 'eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3NTU1MzA2MDcsInVzZXJJZCI6MX0.S4X0YotcNiRqWiWl1DjCWpbflUHdBr1zGwg1uAtyxRA'
                    },
                    body: JSON.stringify({
                        selectionId: parseInt(selectionId)
                    })
                });
                const result = await response.json();
                showResult('text-result', result);
            } catch (error) {
                showResult('text-result', { error: error.message }, true);
            }
        }

        async function testVideoTask() {
            try {
                const selectionId = document.getElementById('video-selection-id').value;
                const refImageId = document.getElementById('ref-image-id').value;
                
                const response = await fetch('http://14.103.244.167:8080/user/video/create-task', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'token': 'eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3NTU1MzA2MDcsInVzZXJJZCI6MX0.S4X0YotcNiRqWiWl1DjCWpbflUHdBr1zGwg1uAtyxRA'
                    },
                    body: JSON.stringify({
                        selectionId: parseInt(selectionId),
                        refImageId: parseInt(refImageId)
                    })
                });
                const result = await response.json();
                showResult('video-task-result', result);
            } catch (error) {
                showResult('video-task-result', { error: error.message }, true);
            }
        }

        async function testQueryStatus() {
            try {
                const userId = document.getElementById('query-user-id').value;
                const taskId = document.getElementById('query-task-id').value;
                
                const response = await fetch(`http://14.103.244.167:8080/user/video/query-task?userId=${userId}&taskId=${taskId}`, {
                    method: 'GET',
                    headers: {
                        'token': 'eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3NTU1MzA2MDcsInVzZXJJZCI6MX0.S4X0YotcNiRqWiWl1DjCWpbflUHdBr1zGwg1uAtyxRA'
                    }
                });
                const result = await response.json();
                showResult('query-result', result);
            } catch (error) {
                showResult('query-result', { error: error.message }, true);
            }
        }
    </script>
</body>
</html>
